package com.lsege.controller.backstage;

import com.github.pagehelper.PageHelper;
import com.lsege.entity.JsonResult;
import com.lsege.entity.Page;
import com.lsege.entity.backstage.RepairMan;
import com.lsege.service.backstage.RepairManService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by lsd on 2017-07-05.
 */
@RestController
@RequestMapping("/repairman")
public class RepairManController {
    @Autowired
    RepairManService repairManService;

    /**
     * 添加维修员
     * @param repairMan
     * @return
     */
    @PostMapping("addRepairMan")
    public JsonResult addRepairMan(RepairMan repairMan){
        JsonResult json = new JsonResult();
        try {
            repairManService.addRepairMan(repairMan);
            json.setSuccess(true);
            json.setMessage("添加信息完成");
        }catch (Exception e){
            json.setSuccess(false);
            json.setMessage("添加信息失败");
        }
        return json;
    }

    /**
     * 获取维修员列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("repairManList")
    public JsonResult RepairManList(Integer pageNum,Integer pageSize){
        JsonResult json = new JsonResult();
        PageHelper.startPage(pageNum,pageSize);
        try{
            List<RepairMan> list = repairManService.RepairManList();
            Long total = repairManService.RepairManListNum();
            json.setData(new Page(list,total,pageSize,pageNum));
            json.setSuccess(true);
            json.setMessage("获取列表成功");
        }catch (Exception e){
            json.setSuccess(false);
            json.setMessage("获取列表失败");
        }
        return json;
    }

    /**
     * 删除维修员
     * @param repairMan
     * @return
     */
    @PostMapping("deleteRepairman")
    public JsonResult deleteRepairman(RepairMan repairMan){
        JsonResult json=new JsonResult();
        Long num=repairManService.getDeleteRepairmanCount(repairMan);
        if (num==0){
            json.setSuccess(false);
            json.setMessage("该配送员不存在");
        }else {
            try {
                repairManService.deleteRepairMan(repairMan);
                json.setSuccess(true);
                json.setMessage("删除成功");
            }catch (Exception e){
                e.printStackTrace();
                json.setSuccess(false);
                json.setMessage("删除失败");
            }
        }
       return json;
    }
}
